package com.example.demo.init;

import com.baidu.bas.core.util.ProjectUtil;
import com.baidu.bas.db.dao.EruptDao;
import com.baidu.bas.upms.constant.MenuTypeEnum;
import com.baidu.bas.upms.model.EruptMenu;
import com.example.demo.model.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2020/12/17
 */
@Service
@Order
@Slf4j
public class TableRegister implements CommandLineRunner {

    @Resource
    private EruptDao eruptDao;

    @Transactional(rollbackOn = Exception.class)
    @Override
    public void run(String... args) throws Exception {
        String code = "code";
        String manager = "$manager";
        Integer open = Integer.valueOf(EruptMenu.OPEN);

        new ProjectUtil().projectStartLoaded("app", first -> {
            if(first){
                log.info("App init. register table");
                Lists.newArrayList(Group.class, SimpleTask.class, GraphTask.class, Channel.class,
                        TaskDailyReport.class,TaskOverviewReport.class,WechatTemplate.class,
                        SmsTemplate.class,Account.class,Channel.class).forEach(aClass->{
                    eruptDao.persistIfNotExist(EruptMenu.class, new EruptMenu(
                            aClass.getSimpleName(), "[" + aClass.getSimpleName() + "]",
                            MenuTypeEnum.API.getCode(), aClass.getSimpleName(), open, 20, null, null
                    ), code, aClass.getSimpleName());
                });

            }
        });
        //TODO 初始化字典表 (来源: interface, enum)
    }
}

